package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.CategoryBean;
import st.business.tepth.business.bean.MaintainWorkBean;
import st.business.tepth.business.bean.PersonBean;
import st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.entity.Lv0;
import st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.entity.Lv1;
import st.business.tepth.manager.UserManager;

/**
 * @author zhangyu
 * @date 2019/7/18
 */
public class StandardWorkAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity,
        BaseViewHolder> {
    public static final int TYPE_0 = 100;
    public static final int TYPE_1 = 101;
    private List<MaintainWorkBean> mCheckList = new ArrayList<>();
    private Drawable mDrawableDone;
    private Drawable mDrawableRaw;
    private OnItemChooseListener<Boolean> mListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public StandardWorkAdapter(Context context, List<MultiItemEntity> data,
                               OnItemChooseListener<Boolean> listener) {
        super(data);
        addItemType(TYPE_0, R.layout.item_maintain_work_categray);
        addItemType(TYPE_1, R.layout.item_maintain_work);
        mDrawableDone = context.getResources().getDrawable(R.drawable.ic_maintain_done);
        mDrawableRaw = context.getResources().getDrawable(R.drawable.ic_arrow_down_yellow);
        mDrawableDone.setBounds(0, 0,
                mDrawableDone.getMinimumWidth(), mDrawableDone.getMinimumHeight());
        mDrawableRaw.setBounds(0, 0,
                mDrawableRaw.getMinimumWidth(), mDrawableRaw.getMinimumHeight());
        mListener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_0:
                final Lv0 lv0 = (Lv0) item;
                CheckBox checkBoxLv0 = helper.getView(R.id.check_box);
                LinearLayout layoutCheckLv0 = helper.getView(R.id.layout_check);
                checkBoxLv0.setChecked(lv0.isChecked());
                TextView tvContentLv0 = helper.getView(R.id.tv_content);
                tvContentLv0.setText(lv0.getName());
                tvContentLv0.setCompoundDrawables(lv0.isAllItemDone() ? mDrawableDone : null,
                        null, mDrawableRaw, null);
                helper.getView(R.id.tv_content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getAdapterPosition();
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
//                            if (pos % 3 == 0) {
//                                expandAll(pos, false);
//                            } else {
                            expand(pos);
//                            }
                        }
                    }
                });
                layoutCheckLv0.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        lv0.setChecked(!lv0.isChecked());
                        List<MaintainWorkBean> itemList =
                                lv0.getMaintainWorkBeans();
                        if (lv0.isChecked()) {
                            //全选
                            if (itemList != null) {
                                for (MaintainWorkBean maintainWorkBean : itemList) {
                                    maintainWorkBean.setChecked(true);
                                    mCheckList.add(maintainWorkBean);
                                }
                                lv0.setCheckCount(itemList.size());
                            }
                        } else {
                            //反选
                            if (itemList != null) {
                                for (MaintainWorkBean maintainWorkBean : itemList) {
                                    maintainWorkBean.setChecked(false);
                                    mCheckList.remove(maintainWorkBean);
                                }
                                lv0.setCheckCount(0);
                            }
                        }
                        changeCbState();
                    }
                });
                break;
            case TYPE_1:
                final Lv1 lv1 = (Lv1) item;
                final MaintainWorkBean maintainWorkBean = lv1.getWorkBean();
                final CheckBox checkBoxLv1 = helper.getView(R.id.check_box);
                TextView tvCancel = helper.getView(R.id.tv_cancel_work);
                LinearLayout layoutCheckLv1 = helper.getView(R.id.layout_check);
                checkBoxLv1.setChecked(maintainWorkBean.isChecked());
                TextView tvContent = helper.getView(R.id.tv_content);
                tvContent.setText(maintainWorkBean.getName());
                if (maintainWorkBean.getState() == 1) {
                    //已完成
                    List<PersonBean> personBeans = maintainWorkBean.getUserList();
                    if (hasMe(personBeans)) {
                        //有自己
                        tvCancel.setVisibility(View.VISIBLE);
                        // checkBoxLv1.setVisibility(View.INVISIBLE);
                        tvContent.setCompoundDrawables(mDrawableDone, null, null, null);
                    } else {
                        tvCancel.setVisibility(View.GONE);
                        // checkBoxLv1.setVisibility(View.VISIBLE);
                        tvContent.setCompoundDrawables(null, null, null, null);
                    }
                } else {
                    tvContent.setCompoundDrawables(null, null, null, null);
                    tvCancel.setVisibility(View.GONE);
                }
                layoutCheckLv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        maintainWorkBean.setChecked(!maintainWorkBean.isChecked());
                        Lv0 parent = lv1.getLv0();
                        if (maintainWorkBean.isChecked()) {
                            mCheckList.add(maintainWorkBean);
                            checkBoxLv1.setChecked(true);
                            parent.setCheckCount(parent.getCheckCount() + 1);
                        } else {
                            mCheckList.remove(maintainWorkBean);
                            checkBoxLv1.setChecked(false);
                            parent.setCheckCount(parent.getCheckCount() - 1);
                        }
                        changeCbState();
                    }
                });
                helper.addOnClickListener(R.id.tv_content);
                helper.addOnClickListener(R.id.tv_cancel_work);
                break;
        }
    }

    private void changeCbState() {
        List<MultiItemEntity> list = getData();
        boolean isCheckAll = true;
        for (MultiItemEntity itemEntity : list) {
            if (itemEntity.getItemType() == TYPE_0) {
                Lv0 lv0 = (Lv0) itemEntity;
                if (!lv0.isChecked()) {
                    isCheckAll = false;
                    break;
                }
            }
        }
        if (mListener != null) {
            mListener.onChoose(isCheckAll);
        }
        notifyDataSetChanged();
    }

    public List<MaintainWorkBean> getCheckList() {
        return mCheckList;
    }

    public void clearSelected() {
        if (mCheckList != null) {
            mCheckList.clear();
        }
    }

    private static boolean hasMe(List<PersonBean> list) {
        if (list != null && !list.isEmpty()) {
            for (PersonBean personBean : list) {
                if (personBean.getJob_no().equals(UserManager.getInstance()
                        .getCurrentUserCode())) {
                    return true;
                }
            }
        }
        return false;
    }

    public static List<MultiItemEntity> convertData(List<CategoryBean> categoryBeans) {
        List<MultiItemEntity> multipleItemEntities = new ArrayList<>();
        if (categoryBeans != null) {
            for (int i = 0, size = categoryBeans.size(); i < size; i++) {
                CategoryBean categoryBean = categoryBeans.get(i);
                String categoryName = categoryBean.getCategory();
                ArrayList<MaintainWorkBean> maintenanceList =
                        categoryBean.getMaintenanceList();
                if (maintenanceList != null && !maintenanceList.isEmpty()) {
                    Lv0 lv0 = new Lv0(categoryName, maintenanceList, false);
                    List<Lv1> subItems = new ArrayList<>();
                    int doSize = 0;
                    for (MaintainWorkBean maintainWorkBean : maintenanceList) {
                        subItems.add(new Lv1(maintainWorkBean, lv0, i));
                        if (maintainWorkBean.getState() == 1
                                && hasMe(maintainWorkBean.getUserList())) {
                            //当前作业已经确认方案，且有自己
                            doSize++;
                        }
                    }
                    lv0.setAllItemDone(doSize == maintenanceList.size());
                    lv0.setSubItems(subItems);
                    multipleItemEntities.add(lv0);
                }

            }
        }
        return multipleItemEntities;
    }

    public void checkAll(boolean isCheckAll) {
        mCheckList.clear();
        List<MultiItemEntity> list = getData();
        for (MultiItemEntity itemEntity : list) {
            if (itemEntity.getItemType() == TYPE_0) {
                Lv0 lv0 = (Lv0) itemEntity;
                lv0.setChecked(isCheckAll);
                List<MaintainWorkBean> maintainWorkBeans = lv0.getMaintainWorkBeans();
                if (maintainWorkBeans != null) {
                    for (MaintainWorkBean maintainWorkBean : maintainWorkBeans) {
                        maintainWorkBean.setChecked(isCheckAll);
                        if (isCheckAll) {
                            //全选
                            mCheckList.add(maintainWorkBean);
                        }
                    }
                    lv0.setCheckCount(isCheckAll ? maintainWorkBeans.size() : 0);
                }
            }
        }
        notifyDataSetChanged();
    }

    public int getConfirmCount() {
        List<MultiItemEntity> dataList = getData();
        int count = -1;
        if (!dataList.isEmpty()) {
            count = 0;
            for (MultiItemEntity entity : dataList) {
                if (entity.getItemType() == TYPE_0) {
                    Lv0 lv0 = (Lv0) entity;
                    List<MaintainWorkBean> maintainWorkBeans = lv0.getMaintainWorkBeans();
                    if (maintainWorkBeans != null) {
                        for (MaintainWorkBean maintainWorkBean : maintainWorkBeans) {
                            if (maintainWorkBean.getState() == 1 &&
                                    hasMe(maintainWorkBean.getUserList())) {
                                count++;
                            }
                        }
                    }
                }
            }
        }
        return count;
    }
}
